package com.hfjyz.base.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hfjyz.base.entity.CodeEntity;
import com.hfjyz.code.bean.CodeBean;

public class CodeUtil {
    public static Map<Integer, List<CodeBean>> codeMap = new HashMap<Integer, List<CodeBean>>();

    public static String getSexString(boolean isMale) {
        return getText(1, isMale ? 1 : 0);
    }

    public static int getSexCode(String sexValue) {
        return getCode(1, sexValue);
    }

    public static String getSexString(Integer sex) {
        return getText(1, sex);
    }

    public static String getEducation(Integer code) {
        return getText(2, code);
    }

    public static int getEducationCode(String value) {
        return getCode(2, value);
    }

    public static String getEducationType(Integer type) {
        return getText(3, type);
    }

    public static int getEducationTypeCode(String value) {
        return getCode(3, value);
    }
    
    public static int getCallAimCode(String value) {
        return getCode(5, value);
    }
    
    public static String getCallAimText(Integer code) {
        return getText(5, code);
    }

    public static void initCodeMap(List<CodeEntity> codeList) {
        codeMap = new HashMap<Integer, List<CodeBean>>();
        for (CodeEntity e : codeList) {
            if (!codeMap.containsKey(e.getType())) {
                codeMap.put(e.getType(), new ArrayList<CodeBean>());
            }
        }

        for (CodeEntity e : codeList) {
            codeMap.get(e.getType()).add(new CodeBean(e));
        }
    }

    public static String getText(Integer type, Integer code) {
        List<CodeBean> codeList = codeMap.get(type);
        for (CodeBean bean : codeList) {
            if (bean.getCode().intValue() == code) {
                return bean.getText();
            }
        }
        return "";
    }

    public static Integer getCode(Integer type, String text) {
        List<CodeBean> codeList = codeMap.get(type);
        for (CodeBean bean : codeList) {
            if (bean.getText().equals(text)) {
                return bean.getCode();
            }
        }
        return -1;
    }

    public static Map<Integer, List<CodeBean>> getCodeMap() {
        return codeMap;
    }
}
